﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Reflection;
using System.Web;

namespace Viewer.BusinessObject.BusinessEntities
{
    [Serializable()]
    public partial class NSTrinhDoChuyenMon : BusinessBase
    {
        CommonCode common = new CommonCode();
        DataClassesDataContext db = new DataClassesDataContext();
        public string TenTrinhDoChuyenMon { set; get; }
        public void EditTDCM(int hdId, int trinhDoID,string ghiChu, DateTime ngayHieuLuc)
        {
            List<NSTrinhDoChuyenMon> lst = (List<NSTrinhDoChuyenMon>)HttpContext.Current.Session["TDCM"];
            NSTrinhDoChuyenMon pc = lst.Where(a => a.TrinhDoChuyenMonID.Equals(hdId)).SingleOrDefault();
            pc.TrinhDoChuyenMonCaoNhatID = trinhDoID;
            pc.GhiChu = ghiChu;
            pc.TenTrinhDoChuyenMon = common.LoadCommonCodeById(trinhDoID);
            if (!ngayHieuLuc.Year.Equals(1))
            {
                pc.NgayHieuLuc = ngayHieuLuc;
            }
          
        }
        public void DeleteTDCM(int hdId)
        {
            List<NSTrinhDoChuyenMon> lst = (List<NSTrinhDoChuyenMon>)HttpContext.Current.Session["TDCM"];
            NSTrinhDoChuyenMon pc = lst.Where(a => a.TrinhDoChuyenMonID.Equals(hdId)).SingleOrDefault();
            lst.Remove(pc);
        }
        public void DeleletAllTDCMByNhanVien(int nhanVienId)
        {
            var query = (from a in db.NSTrinhDoChuyenMons
                         where a.NhanVienID.Equals(nhanVienId)
                         select a).ToList();
            if (query.Count > 0)
            {
                db.NSTrinhDoChuyenMons.DeleteAllOnSubmit(query);
                db.SubmitChanges();
            }

        }
        public void UpdateTDCM_NhanVien(int nhanVienId, List<NSTrinhDoChuyenMon> lstObject)
        {

            var queryNBLByNhanVienId = (from a in db.NSTrinhDoChuyenMons
                                        where a.NhanVienID.Equals(nhanVienId)
                                        select a).ToList();
            if (queryNBLByNhanVienId.Count > 0)
            {
                db.NSTrinhDoChuyenMons.DeleteAllOnSubmit(queryNBLByNhanVienId);
                db.SubmitChanges();
            }

            foreach (NSTrinhDoChuyenMon pc in lstObject)
            {
                NSTrinhDoChuyenMon item = new NSTrinhDoChuyenMon();
                CopyNSTrinhDoChuyenMon(pc, item);
                db.NSTrinhDoChuyenMons.InsertOnSubmit(item);
            }
            db.SubmitChanges();

        }
        public List<NSTrinhDoChuyenMon> LoadTDCMByNhanVienId(int nhanVienId)
        {

            var query = (from a in db.NSTrinhDoChuyenMons
                         where a.NhanVienID.Equals(nhanVienId)
                         
                         select a).ToList();
            foreach (NSTrinhDoChuyenMon item in query)
            {
                item.TenTrinhDoChuyenMon = common.LoadCommonCodeById((int)item.TrinhDoChuyenMonCaoNhatID);
            }

            return query;

        }
        public NSTrinhDoChuyenMon GetNSTrinhDoChuyenMon(int nhanVienId, int trinhDoID, string ghiChu, DateTime ngayHieuLuc)
        {
            var db = new DataClassesDataContext();
            NSTrinhDoChuyenMon hd = new NSTrinhDoChuyenMon();
            hd.TrinhDoChuyenMonID = 0 - DateTime.Now.Second;
            hd.GhiChu = ghiChu;
            hd.NhanVienID = nhanVienId;
            hd.TrinhDoChuyenMonCaoNhatID = trinhDoID;
            hd.TenTrinhDoChuyenMon = common.LoadCommonCodeById(trinhDoID);
            if (!ngayHieuLuc.Year.Equals(1))
            {
                hd.NgayHieuLuc = ngayHieuLuc;
            }
            return hd;
        }
        private void CopyNSTrinhDoChuyenMon(NSTrinhDoChuyenMon src, NSTrinhDoChuyenMon des)
        {
            des.GhiChu = src.GhiChu;
            des.NhanVienID = src.NhanVienID;
            des.TrinhDoChuyenMonCaoNhatID = src.TrinhDoChuyenMonCaoNhatID;
            des.TenTrinhDoChuyenMon = src.TenTrinhDoChuyenMon;
           
            if (src.NgayHieuLuc.HasValue)
            {
                des.NgayHieuLuc = src.NgayHieuLuc;
            }
        }
    }
}